<?php

namespace Home\Controller;

use Home\Model\LogsModel;

class AppController extends WechatController
{
    protected $needAuth = true;
    protected $config = array('app_type' => 'none');

    public function qrlogin($randomStr, $allow = null)
    {
        $userId = session(C("USER_AUTH_KEY"));
        $log = D('Logs')->where(['model' => 'qrlogin_'.$randomStr])->find();

        if (is_null($allow) or !$log) {
            (new LogsModel())->log($userId, 'qrlogin_'.$randomStr, [
                'content' => '二维码登录',
                'isAllow' => false,
                'randomStr' => $randomStr,
                'userId' => $userId,
            ]);
        } else {
            $allow = ($allow == '1');
            if ($allow) {
                $log['content'] = json_encode([
                    'content' => '二维码登录',
                    'isAllow' => $allow,
                    'randomStr' => $randomStr,
                    'userId' => $userId,
                ]);

                $log['updated_at'] = time();

                (new LogsModel())->save($log);
            }
        }

        $this->assign('title', '二维码登录');
        $this->assign('allow', $allow);
        $this->assign('phone', session('user_name'));
        $this->assign('randomStr', $randomStr);
        $this->assign('jsapiPackage', $this->getQywechat()->getJsApiPackage());

        $this->display('qrlogin.mobile');
    }
}
